package hello;

import org.hibernate.Session;
import org.hibernate.Transaction;

import persistence.HibernateUtil;
import auction.chapter08.BillingAddressJoinTable;
import auction.chapter08.DailyBillingInverseJoinProp;
import auction.chapter08.ItemInverseJoinProp;
import auction.chapter08.UserJoinTable;

public class HelloWorldJoinTable
{
	public static void main(String[] args)throws Exception{
		inverseJoinProp();
	}
	
	public static void inverseJoinProp()throws Exception{
		Session s=HibernateUtil.getSessionFactory().openSession();
		Transaction tx=s.beginTransaction();
		
		ItemInverseJoinProp i=new ItemInverseJoinProp();
		i.setTitle("iii");
		i.setBillingTotal(2.34);
		
		DailyBillingInverseJoinProp b=new DailyBillingInverseJoinProp();
		b.setStatus("ok");
		b.setTotal(1.23);
		b.setItem(i);
		
		//s.save(i);
		//s.save(b);
		
		tx.commit();
		s.close();
		
		
		
		Session s1=HibernateUtil.getSessionFactory().openSession();
		Transaction tx1=s1.beginTransaction();
		//update a detached object will cause update sql even this didn't modify
		//s1.update(i);
		
		ItemInverseJoinProp loadi=(ItemInverseJoinProp)s1.load(ItemInverseJoinProp.class, 1L);
		out(loadi.getBillingTotal());
		
		tx1.commit();
		s1.close();
		
		
		
		//HibernateUtil.stat();
		HibernateUtil.shutdown();
	}
	
	public static void jointable ()throws Exception
	{
		
		Session s=HibernateUtil.getSessionFactory().openSession();
		Transaction tx=s.beginTransaction();
		
		
		
		UserJoinTable u=new UserJoinTable();
		u.setName("wei2");
		
		BillingAddressJoinTable b=new BillingAddressJoinTable();
		b.setCity("Cheng Du");
		b.setStreet("second ring road");
		b.setZipcode("610041");
		
		u.setBillingAddress(b);
		//s.save(u);
		
		tx.commit();
		s.close();
		
		
		
		Session s1=HibernateUtil.getSessionFactory().openSession();
		Transaction tx1=s1.beginTransaction();
		
		UserJoinTable loadedu=(UserJoinTable)s1.load(UserJoinTable.class, 3L);
		out(loadedu.getBillingAddress().getCity());
		//out(loadedu.getName());
		tx1.commit();
		s1.close();
		
		
		
		//HibernateUtil.stat();
		HibernateUtil.shutdown();
	}
	
	public static void getParent(Class clazz)
	{
		Class parent=clazz.getSuperclass();
	  if(parent !=null)
	  {
			System.out.println("###"+parent.getName());
			getParent(parent);
		}
	}
	
	static void out(Object obj)
	{
		System.out.println("--->"+obj);
	}
}
